<?php
	/* ***************************************************** */
	/* Arcans Team (2010), website                           */
	/* Site under GNU GPL v3 Licence                         */
	/* (see NOTICE and COPYING in the of the site file tree) */
	/* ***************************************************** */

	/* ***************************************************** */
	/* Fonction de simplification pour les BDD               */
	/* ***************************************************** */
	function bdd_connect($type,$conf) {
		$xml_bdd = new DomDocument(); $xml_bdd->load($conf[5]); $bdd = $xml_bdd->getElementsByTagName("bdd");
		foreach ($bdd as $elt) if ($elt->getAttribute("name")==$type) { $bdd = $elt; break; }
		$bdd = new PDO ($bdd->getAttribute("value0"),$bdd->getAttribute("value1"),$bdd->getattribute("value2"));
		return $bdd;
	}
	/* ***************************************************** */
	/* Fonction de simplification pour le XML                */
	/* ***************************************************** */
	function addNode($document,$type,$parent,$attr_tab) {
		$node = $document->createElement($type);
		foreach ($attr_tab as $key => $element) {
			if ($key == "texte") $node->appendChild($document->createTextNode($element));
			elseif ($key == "onclick") $node->setAttribute($key,"javascript:".$element);
			else $node->setAttribute($key,$element);
		}
		$parent->appendChild($node);
		return $node;
	}
	/* ***************************************************** */
	/* Fonction principale de gestion des requêtes AJAX      */
	/* ***************************************************** */
	function AJAX_Fun($conf) {
		$document = DOMImplementation::createDocument('','ajax');
		$document->encoding = 'utf-8';
		if (htmlentities($_GET["ajax"]) == "decon") { $_SESSION = array(); session_destroy(); }
		else {
			$ajax_xml = new DomDocument(); $ajax_xml->load($conf[3]."ajax.xml");
			$listajax = $ajax_xml->getElementsByTagName("ajax");
			foreach ($listajax as $elt) {
				if (htmlentities($_GET["ajax"]) == $elt->getAttribute("get")) {
					require_once($conf[$elt->getAttribute("php_path")].$elt->getAttribute("php_file"));
					$fun = $elt->getAttribute("fun"); $fun($document,$conf); break;
				}
			}
		}
		//mise en forme du flux d'envoi
		ob_start("ob_gzhandler");
		header('Content-Type: application/xml');
		header("Expires: Mon, 26 Jul 1997 05:00:00 Paris");
		header("Last-Modified: ".date("D, d M Y H:i:s")." Paris");
		header("Cache-Control: no-store, no-cache, must-revalidate");
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Pragma: no-cache");
		echo $document->saveXML(); //envoi des données
	}
	/* ***************************************************** */
	/* Fonction principale pour la gestion de l'affichage    */
	/* ***************************************************** */
	function Affichage_Fun($conf) {
		$page = array("",""); $req_page = "";
		if (isset($_GET["p"])) $req_page = htmlentities($_GET["p"]);
		if ($req_page == "") $req_page = "index";
		$site_xml = new DomDocument(); $site_xml->load($conf[3]."pages.xml");
		$listpages = $site_xml->getElementsByTagName("page");
		//recherche des chemins de données de la page demandée
		$max_lvl = 1; $min_lvl = 0;
		if (isset($_SESSION["level"])) { $max_lvl = $_SESSION["level"]; $min_lvl = 1; }
		foreach ($listpages as $element) {
			if ($element->getAttribute("get") == $req_page) {
				//vérification page/level
				if ($element->getAttribute("level") <= $max_lvl && $element->getAttribute("level") >= $min_lvl) {
					$page[0] = $conf[$element->getAttribute("path")].$element->getAttribute("href");
					$page[1] = $element->getAttribute("name");
					$page[2] = $conf[$element->getAttribute("php_path")].$element->getAttribute("php_file");
				}
				else { $page[0] = $conf[3]."erreur.xml"; $page[1] = "Erreur !"; }
				break;
			}
		}
		//chargement des données de la page demandée
		if ($page[0] == "") { $page[0] = $conf[3]."erreur.xml"; $page[1] = "Erreur !"; }
		$xml_page = new DomDocument(); $xml_page->load($page[0]);
		$xml_head = $xml_page->getElementsByTagName("head")->item(0);
		$xml_body = $xml_page->getElementsByTagName("body")->item(0);
		$xml_sources = $xml_page->getElementsByTagName("sources")->item(0);
		if ($xml_sources->hasChildNodes()) {
			require_once($page[2]);
			$listfun = $xml_page->getElementsByTagName("function");
			foreach ($listfun as $function) {
				$fun = $function->getAttribute("name");
				$attr_tab = array(); $i = 0;
				while ($function->hasAttribute("value".$i)) {
					array_push($attr_tab,$function->getAttribute("value".$i)); $i++;
				}
				$return = $fun($conf,$attr_tab); if ($return["mod"]) mod_xml($xml_page,$xml_page->documentElement,$return);
			}
		}
		//mise à jour de l'entête (head)
		addNode($xml_page,"link",$xml_head,array("rel"=>"icon","type"=>"image.png","href"=>"favAT.png"));
		addNode($xml_page,"title",$xml_head,array("texte"=>"Arcans Team - ".$page[1]));
		addNode($xml_page,"script",$xml_head,array("type"=>"text/javascript","src"=>"javascript/base.js"));
		if (isset($_SESSION["pseudo"]))
			addNode($xml_page,"script",$xml_head,array("type"=>"text/javascript","src"=>"javascript/decon.js"));
		else addNode($xml_page,"script",$xml_head,array("type"=>"text/javascript","src"=>"javascript/connex.js"));
		$mod = $xml_head->getElementsByTagName("link");
		foreach ($mod as $element) $element->setAttribute("href",$conf[2].$element->getAttribute("href"));
		$mod = $xml_head->getElementsByTagName("script");
		foreach ($mod as $element) $element->setAttribute("src",$conf[2].$element->getAttribute("src"));
		addNode($xml_page,"script",$xml_head,array("type"=>"text/javascript","texte"=>'var absolute_path = "'.$conf[2].'";'));
		//mise à jour du titre
		if ($xml_page->getElementsByTagName("header")->length != 0) {
			$div_titre = $xml_page->getElementsByTagName("header")->item(0);
			$node = addNode($xml_page,"div",$div_titre,array("class"=>"title_t"));
			addNode($xml_page,"h1",$node,array("texte"=>"Arcans Team"));
			addNode($xml_page,"h2",$node,array("texte"=>$page[1]));
			$div_t = addNode($xml_page,"div",$div_titre,array("class"=>"title_i","height"=>"150px","width"=>"150px"));
			$svgf = new DomDocument(); $svgf->load("svg/logoAT.svg");
			$svg = $svgf->getElementsByTagName("svg")->item(0);
			$div_t->appendChild($xml_page->importNode($svg,true));
			//balise <noscript>
			if ($_GET["p"] != "nojs") {
				$texte = "Le javascript est actuellement désactivé sur votre navigateur.";
				$noscript = addNode($xml_page,"noscript",$div_titre,array("texte"=>$texte));
				addNode($xml_page,"br",$noscript,array());
				addNode($xml_page,"a",$noscript,array("texte"=>"Voir en quoi est-ce important ?","href"=>"?p=nojs"));
			}
		}
		//mise à jour du menu (nav)
		if ($xml_page->getElementsByTagName("nav")->length != 0) {
			$nav = $xml_body->getElementsByTagName("nav")->item(0);
			$xml_menu = new DomDocument(); $xml_menu->load($conf[3]."/menu.xml");
			$menu = $xml_menu->getElementsByTagName("link");
			foreach ($menu as $element) {
				if ($element->getAttribute("page") != $req_page) {
					if ($element->getAttribute("level") <= $max_lvl && $element->getAttribute("level") >= $min_lvl) {
						addNode($xml_page,"a",$nav,array("href"=>$conf[2].$element->getAttribute("page"),"texte"=>$element->getAttribute("name")));
						$nav->appendChild($xml_page->createTextNode(" | "));
					}
				}
			}
			if (isset($_SESSION["pseudo"])) addNode($xml_page,"a",$nav,array("texte"=>"Déconnexion","href"=>"#","onclick"=>"decon.effect(0);"));
			else addNode($xml_page,"a",$nav,array("texte"=>"Connexion","href"=>"#","onclick"=>"connex.init();"));
		}
		//mise à jour du footer, s'il existe
		if ($xml_page->getElementsByTagName("footer")->length != 0) logos($xml_page,$xml_page->getElementsByTagName("footer")->item(0),$conf);
		//mise en place du voile
		addNode($xml_page,"div",$xml_body,array("id"=>"voile_ex","style"=>"display:none;")); $voile=$conf[2]."javascript/voile.js";
		if($xml_page->getElementsByTagName("script")->item(0)->getAttribute("src")==$voile) $voile=true; else $voile=false;
		if ($voile) {
			$div = addNode($xml_page,"div",$xml_body,array("id"=>"voile_P_in","style"=>"display:none;width:10px;height:10px;"));
			$af1 = addNode($xml_page,"div",$div,array("id"=>"voile_af1","style"=>"display:none;"));
			addNode($xml_page,"div",$xml_body,array("id"=>"voile_verr","style"=>"display:none;"));
			$div = addNode($xml_page,"div",$xml_body,array("id"=>"voile_X_in","style"=>"display:none;width:10px;height:10px;"));
			$af3 = addNode($xml_page,"div",$div,array("id"=>"voile_af3","style"=>"display:none;"));
			addNode($xml_page,"div",$af1,array("id"=>"enc1_all","style"=>"display:none;","class"=>"encart_af1"));
			addNode($xml_page,"div",$af3,array("id"=>"enc3_all","style"=>"display:none;","class"=>"encart_af3"));
			$enc1_supp = $xml_page->getElementsByTagName("af1");
			while ($enc1_supp->length > 0) {
				$elt = $enc1_supp->item(0);
				$new = addNode($xml_page,"div",$af1,array("id"=>$elt->getAttribute("id"),"style"=>"display:none;","class"=>"encart_af1"));
				while ($elt->hasChildNodes()) $new->appendChild($elt->firstChild); $elt->parentNode->removeChild($elt);
			}
			$enc3_supp = $xml_page->getElementsByTagName("af3");
			while ($enc3_supp->length > 0) {
				$elt = $enc3_supp->item(0);
				$new = addNode($xml_page,"div",$af3,array("id"=>$elt->getAttribute("id"),"style"=>"display:none;","class"=>"encart_af3"));
				while ($elt->hasChildNodes()) $new->appendChild($elt->firstChild); $elt->parentNode->removeChild($elt);
			}
		}
		$div = addNode($xml_page,"div",$xml_body,array("id"=>"voile_S_in","style"=>"display:none;top:-281px;"));
		$af2 = addNode($xml_page,"div",$div,array("id"=>"voile_af2","style"=>"display:none;"));
		addNode($xml_page,"div",$af2,array("id"=>"enc2_all","style"=>"display:none;","class"=>"encart_af2"));
		$enc2_supp = $xml_page->getElementsByTagName("af2");
		while ($enc2_supp->length > 0) {
			$elt = $enc3_supp->item(0);
			$new = addNode($xml_page,"div",$af2,array("id"=>$elt->getAttribute("id"),"style"=>"display:none;","class"=>"encart_af2"));
			while ($elt->hasChildNodes()) $new->appendChild($elt->firstChild); $elt->parentNode->removeChild($elt);
		}
		if ($voile) {
			$tab_a = "display:none;left:-360px;";
			$tab_a = array("id"=>"voile_lg1","style"=>$tab_a,"data"=>$conf[2]."svg/voile_lg.svg","type"=>"application/xml");
			addNode($xml_page,"object",$xml_body,$tab_a);
		}
		$tab_a = "display:none;right:390px;opacity:0;";
		$tab_a = array("id"=>"voile_lg2","style"=>$tab_a,"data"=>$conf[2]."svg/logoAT.svg","type"=>"application/xml");
		addNode($xml_page,"object",$xml_body,$tab_a);
		//ajout des encarts de connexion / déconnexion
		if (isset($_SESSION["level"])) {
			$encart = addNode($xml_page,"div",$af2,array("id"=>"encart_prev","style"=>"display:none;","class"=>"encart_af2"));
			addNode($xml_page,"h5",$encart,array("texte"=>"Attention !")); $texte = "Vous allez être automatiquement déconnecté dans : ";
			$p = addNode($xml_page,"p",$encart,array("texte"=>$texte,"style"=>"text-align:center;","class"=>"error"));
			addNode($xml_page,"span",$p,array("texte"=>"30 secondes","id"=>"sec_decon_span"));
			$p = addNode($xml_page,"div",$encart,array("style"=>"text-align:center;"));
			addNode($xml_page,"button",$p,array("texte"=>"Déconnexion","onclick"=>"window.clearTimeout(reload_timer);decon.effect(1);"));
			$js = "var click=2;if(doc.byID('voile_P_in')!=null&&voile.voile_P_in().style.display=='block')click=0;window.clearTimeout";
			$js .= "(reload_timer);reload_timer=window.setTimeout('decon.prev();',1500000);voile.initSclose(click,'encart_prev');";
			addNode($xml_page,"button",$p,array("texte"=>"Annuler","onclick"=>$js));
			$encart = addNode($xml_page,"div",$af2,array("id"=>"encart_decon","style"=>"display:none;","class"=>"encart_af2"));
			addNode($xml_page,"h5",$encart,array("texte"=>"Déconnexion"));
			addNode($xml_page,"p",$encart,array("class"=>"good","style"=>"margin-top:90px;","texte"=>"Vous êtes maintenant déconnecté."));
			addNode($xml_page,"p",$encart,array("style"=>"color:green;","texte"=>"La page va se recharger automatiquement."));
		}
		else {
			$encart = addNode($xml_page,"div",$af2,array("id"=>"encart_connex","style"=>"display:none;","class"=>"encart_af2"));
			addNode($xml_page,"h5",$encart,array("texte"=>"Connexion :")); addNode($xml_page,"p",$encart,array("id"=>"alert_con"));
			$table = addNode($xml_page,"table",$encart,array("style"=>"margin:5px;width:290px;"));
			$cell = addNode($xml_page,"td",addNode($xml_page,"tr",$table,array()),array());
			addNode($xml_page,"label",$cell,array("texte"=>"Pseudonyme","for"=>"name_con"));
			$cell = addNode($xml_page,"td",addNode($xml_page,"tr",$table,array()),array("style"=>"text-align:right;"));
			addNode($xml_page,"input",$cell,array("type"=>"text","id"=>"name_con","maxlength"=>"15"));
			$cell = addNode($xml_page,"td",addNode($xml_page,"tr",$table,array()),array());
			addNode($xml_page,"label",$cell,array("texte"=>"Mot de passe :","for"=>"pwd_con"));
			$cell = addNode($xml_page,"td",addNode($xml_page,"tr",$table,array()),array("style"=>"text-align:right;"));
			addNode($xml_page,"input",$cell,array("type"=>"password","id"=>"pwd_con","maxlength"=>"15"));
			$div = addNode($xml_page,"div",$encart,array("id"=>"divbts_con","style"=>"margin:8px;"));
			addNode($xml_page,"button",$div,array("id"=>"connexb","texte"=>"Connexion","onclick"=>"connex.go_con();"));
			addNode($xml_page,"button",$div,array("id"=>"closeb","texte"=>"Annuler","onclick"=>"voile.initSclose(2,'encart_connex');"));
		}
		//mise en forme du flux d'envoi
		ob_start("ob_gzhandler");
		header('Content-Type: application/xml');
		header("Expires: Mon, 26 Jul 1997 05:00:00 Paris");
		header("Last-Modified: ".date("D, d M Y H:i:s")." Paris");
		header("Cache-Control: no-store, no-cache, must-revalidate");
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Pragma: no-cache");
		$document = DOMImplementation::createDocument("http://www.w3.org/1999/xhtml","html");
		$document->encoding = 'utf-8'; $html = $document->documentElement;
		$html->appendChild($document->importNode($xml_head, true));
		if ($xml_body->hasChildNodes()) $html->appendChild($document->importNode($xml_body, true));
		echo $document->saveXML(); //envoi des données
	}
	/* ***************************************************** */
	/* fonction d'insertion des logos dans le footer         */
	/* ***************************************************** */
	function logos ($xml_page,$footer,$conf) {
		$choix = $footer->getElementsByTagName("choix")->item(0); $xml_logos = new DomDocument();
		$xml_logos->load("xml/choix_logos.xml"); $logos = $xml_logos->getElementsByTagName("data")->item(0);
		$choix_i = $choix->getAttribute("name"); $logos_choix = $xml_logos->getElementsByTagName("choix");
		foreach ($logos_choix as $elt) if ($elt->getAttribute("name") == $choix_i) { $choix_i = $elt; break; }
		$row = addNode($xml_page,"tr",addNode($xml_page,"table",$footer,array("style"=>"width:auto;")),array());
		//LOGO V0.1
		$cell = addNode($xml_page,"td",$row,array());
		$svgf = new DomDocument(); $svgf->load("logos/v0-1.svg"); $svg = $svgf->documentElement;
		$cell->appendChild($xml_page->importNode($svg,true));
		//LOGO GIT
		$link = array("href"=>"http://git-scm.com/","target"=>"_blank");
		$cell = addNode($xml_page,"a",addNode($xml_page,"td",$row,array()),$link);
		$svgf->load("logos/Git_icon.svg"); $svg = $svgf->documentElement;
		$cell->appendChild($xml_page->importNode($svg,true));
		//LOGO googlecode project hosting
		$link = array("href"=>"http://code.google.com/p/arcans-team/","target"=>"_blank");
		$cell = addNode($xml_page,"a",addNode($xml_page,"td",$row,array()),$link);
		addNode($xml_page,"img",$cell,array("src"=>$conf[2]."logos/logo_googlecode.png"));
		//LOGO GPL V3
		$link = array("href"=>"http://www.gnu.org/licenses/gpl.html","target"=>"_blank");
		$cell = addNode($xml_page,"a",addNode($xml_page,"td",$row,array()),$link);
		$svgf->load("logos/gpl-v3-logo.svg"); $svg = $svgf->documentElement;
		$cell->appendChild($xml_page->importNode($svg,true));
		//LOGO PHP
		if ($choix_i->getAttribute("php")) {
			$link = array("href"=>"http://fr.php.net","target"=>"_blank");
			$cell = addNode($xml_page,"a",addNode($xml_page,"td",$row,array()),$link);
			$svgf->load("logos/PHP-logo.svg"); $svg = $svgf->documentElement;
			$cell->appendChild($xml_page->importNode($svg,true));
		}
		//LOGO HTML 5
		if ($choix_i->getAttribute("html5")) {
			$link = array("href"=>"http://www.w3.org/html/logo/","target"=>"_blank");
			$cell = addNode($xml_page,"a",addNode($xml_page,"td",$row,array("width"=>"39","height"=>"39")),$link);
			$svgf->load("logos/HTML5_Badge.svg"); $svg = $svgf->documentElement;
			$cell->appendChild($xml_page->importNode($svg,true));
		}
		//LOGO COLLADA
		if ($choix_i->getAttribute("collada")) {
			$link = array("href"=>"http://www.khronos.org/collada/","target"=>"_blank");
			$cell = addNode($xml_page,"a",addNode($xml_page,"td",$row,array()),$link);
			addNode($xml_page,"img",$cell,array("src"=>$conf[2]."logos/COLLADA_logo.png","width"=>"80px"));
		}
		//LOGO WEBGL
		if ($choix_i->getAttribute("webgl")) {
			$link = array("href"=>"http://www.khronos.org/webgl/","target"=>"_blank");
			$cell = addNode($xml_page,"a",addNode($xml_page,"td",$row,array("width"=>"50","height"=>"50")),$link);
			addNode($xml_page,"img",$cell,array("src"=>$conf[2]."logos/WebGL_logo.png","width"=>"80px"));
		}
		//LOGO SVG
		if ($choix_i->getAttribute("svg")) {
			$link = array("href"=>"http://www.w3.org/Graphics/SVG/","target"=>"_blank");
			$cell = addNode($xml_page,"a",addNode($xml_page,"td",$row,array("width"=>"39","height"=>"39")),$link);
			$svgf->load("logos/svg-logo-v.svg"); $svg = $svgf->documentElement;
			$cell->appendChild($xml_page->importNode($svg,true));
		}
		//LOGO JAVA
		if ($choix_i->getAttribute("java")) {
			$link = array("href"=>"http://www.java.net/","target"=>"_blank");
			$cell = addNode($xml_page,"a",addNode($xml_page,"td",$row,array()),$link);
			$svgf->load("logos/Logo_java.svg"); $svg = $svgf->documentElement;
			$cell->appendChild($xml_page->importNode($svg,true));
		}
		//LOGO SQLITE
		if ($choix_i->getAttribute("sqlite")) {
			$link = array("href"=>"http://www.sqlite.org/","target"=>"_blank");
			$cell = addNode($xml_page,"a",addNode($xml_page,"td",$row,array()),$link);
			$svgf->load("logos/sqlite_logo.svg"); $svg = $svgf->documentElement;
			$cell->appendChild($xml_page->importNode($svg,true));
		}
		//LOGO PostGreSQL
		if ($choix_i->getAttribute("pgsql")) {
			$link = array("href"=>"http://www.postgresql.org/","target"=>"_blank");
			$cell = addNode($xml_page,"a",addNode($xml_page,"td",$row,array()),$link);
			$svgf->load("logos/pgsql_logo.svg"); $svg = $svgf->documentElement;
			$cell->appendChild($xml_page->importNode($svg,true));
		}
		//LOGO MySQL
		if ($choix_i->getAttribute("mysql")) {
			$link = array("href"=>"http://www.mysql.fr","target"=>"_blank");
			$cell = addNode($xml_page,"a",addNode($xml_page,"td",$row,array()),$link);
			$svgf->load("logos/MySQL.svg"); $svg = $svgf->documentElement;
			$cell->appendChild($xml_page->importNode($svg,true));
		}
		//LOGO XML
		if ($choix_i->getAttribute("xml")) {
			$link = array("href"=>"http://www.w3.org/TR/2008/REC-xml-20081126/","target"=>"_blank");
			$cell = addNode($xml_page,"a",addNode($xml_page,"td",$row,array()),$link);
			$svgf->load("logos/xml_logo.svg"); $svg = $svgf->documentElement;
			$cell->appendChild($xml_page->importNode($svg,true));
		}
		//fin
		$footer->removeChild($choix);
	}
	/* ***************************************************** */
	/* fonction de modification du xml de la page            */
	/* ***************************************************** */
	function mod_xml ($xml_page,$xml_zone,$return) {
		$codes = $xml_zone->getElementsByTagName("cod"); $iter = 0; $else = false;
		while ($codes->length != 0 && $codes->length > $iter) {
			if ($codes->item($iter)->getAttribute("f") == $return["fun"]) {
				$code = $codes->item($iter);
				switch ($code->getAttribute("n")) {
					case "i": //if
						if ($else) $else = false;
						if ($return[$code->getAttribute("c")] == $code->getAttribute("v"))
							while ($code->hasChildNodes()) $code->parentNode->insertBefore($code->firstChild,$code);
						else $else = true;
						break;
					case "ei": //elseif
						if ($else && $return[$code->getAttribute("c")] == $code->getAttribute("v")) { $else = false;
							while ($code->hasChildNodes()) $code->parentNode->insertBefore($code->firstChild,$code); } break;
					case "e": //else
						if ($else) { $else=false; while($code->hasChildNodes()) $code->parentNode->insertBefore($code->firstChild,$code); } break;
					case "f": //for
						if ($else) $else = false;
						for ($eter=0;$eter<count($return[$code->getAttribute("v")]);$eter++) {
							$elt = $return[$code->getAttribute("v")][$eter]; $clone = $code->cloneNode(true);
							mod_xml($xml_page,$clone,$elt); while ($clone->hasChildNodes()) $code->parentNode->appendChild($clone->firstChild);
						} break;
					case "r": //replace
						switch ($code->getAttribute("tp")) {
							case "x": //xml
								$xml_r = addNode($xml_page,$code->getAttribute("tag"),$code->parentNode,$return[$code->getAttribute("v")]);
								while ($code->hasChildNodes()) $xml_r->appendChild($code->firstChild); break;
							case "t": //texte
								$code->parentNode->insertBefore($xml_page->createTextNode($return[$code->getAttribute("v")]),$code); break;
						} break;
				} $code->parentNode->removeChild($code);
			} else $iter++;
		}
	}
	/* ***************************************************** */
	/* Script d'initialisation                               */
	/* ***************************************************** */
	session_start();
	if (isset($_GET["ajax"])) AJAX_Fun($conf);
	else Affichage_Fun($conf);
?>